package com.initial.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.initial.Utils.SendMessageUtil;
import com.initial.entity.TbCrmContacts;
import com.initial.entity.TbCrmCustomer;
import com.initial.entity.TbSystemUser;
import com.initial.mapper.TbCrmCustomerMapper;
import com.initial.mapper.TbSystemUserMapper;
import com.initial.sendemail.EmailServiceImpl;
import com.initial.service.impl.TbCrmContactsServiceImpl;
import com.initial.service.impl.TbCrmCustomerServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class Customer {
    @Resource
    private TbCrmCustomerServiceImpl tbCrmCustomerService;
    @Resource
    private TbSystemUserMapper userMapper;
    @Resource
    private TbCrmContactsServiceImpl tbCrmContactsService;
    @Resource
    private EmailServiceImpl emailService;
    @Resource
    private TbCrmCustomerMapper crmCustomerMapper;

    //去客户模块
    @RequestMapping("/customer")
    public String customer() {
        return "/views/manage/customer/customer";
    }

    //去展示页面
    @RequestMapping("/toList")
    public String toList() {
        return "/views/manage/customer/list";
    }

    //去客户池
    @RequestMapping("/toPool")
    public String toPool() {
        return "/views/manage/customer/pool";
    }

    //查询没有拥有者的客户 返回到页面
    @RequestMapping("/lisPool")
    @ResponseBody
    public Map lisPool(Integer page, Integer rows) {
        System.out.println(page + rows);
        Map map = new HashMap();
        IPage<TbCrmCustomer> selectPool = crmCustomerMapper.selectPool(new Page<>(page, rows));
        map.put("total", selectPool.getTotal());//查出来的总数量
        map.put("rows", selectPool.getRecords());//查出来的总数据
        System.out.println(selectPool.getRecords());
        return map;
    }

    //展示数据到list页面，模糊传销
    @RequestMapping("/lis")
    @ResponseBody
    public Map seeList(Integer page, Integer rows, HttpSession session) {
        Map map = new HashMap();
        /*String type1 = (String) session.getAttribute("type1");
        String type2 = (String) session.getAttribute("type2");

        if (type1 != null && type1.equals("day")) {
            System.out.println("1111111111111111");
            IPage<TbCrmCustomer> todayCreate = crmCustomerMapper.todayCreate(type1, new Page<>(page, rows));
            session.removeAttribute("type1");
            map.put("total", todayCreate.getTotal());
            map.put("rows", todayCreate.getRecords());
            return map;
        }

        if (type2 != null && type2.equals("week")) {
            System.out.println("2222222222222222222");
            IPage<TbCrmCustomer> weekCreate = crmCustomerMapper.weekCreate(type2, new Page<>(page, rows));
            session.removeAttribute("type2");
            map.put("total", weekCreate.getTotal());
            map.put("rows", weekCreate.getRecords());
            return map;
        }*/
        String type = (String) session.getAttribute("type");//获取存在session的type（day、week、month）
        if (type != null) {//如果不为空 执行方法
            IPage<TbCrmCustomer> time = crmCustomerMapper.createTime(type, new Page<>(page, rows));
            session.removeAttribute("type");
            map.put("total", time.getTotal());//查出来的总数量
            map.put("rows", time.getRecords());//查出来的总数据
            return map;//返回出去
        }
        String filterSearch = (String) session.getAttribute("filterSearch");//获取session值
        String search = (String) session.getAttribute("search");
        String lname = null;
        String addr = null;
        String time = null;
        String industry = null;
        //判断查询条件为哪个  然后赋值
        if ("lname".equals(filterSearch)) {
            lname = search;
        }
        if ("addr".equals(filterSearch)) {
            addr = search;
        }
        if ("time".equals(filterSearch)) {
            time = search;
        }
        if ("industry".equals(filterSearch)) {
            industry = search;
        }
        IPage<TbCrmCustomer> iPage = tbCrmCustomerService.selectCustomer(lname, addr, time, industry, new Page<>(page, rows));//查出来的数据  查询条件为空时，查询全部，即按分页查询
        List<TbCrmCustomer> list = iPage.getRecords();//查出来的具体数据
        long total = iPage.getTotal();//总数据量
        map.put("total", total);
        map.put("rows", list);
        session.removeAttribute("filterSearch");//清除session
        session.removeAttribute("search");
        return map;
    }

    //模糊
    @RequestMapping("/likeCustomer")
    public String likeCustomer(String filterSearch, String search, HttpSession session) {
        session.setAttribute("filterSearch", filterSearch);//查询条件的name放入session
        session.setAttribute("search", search);//搜索按钮的name放入session
        return "/views/manage/customer/list"; //再次打回list页面
    }

    //查看客户
    @RequestMapping("/customerVO")
    public String show(Integer id, HttpSession session, ModelMap modelMap) {
        System.out.println(id);
        TbCrmCustomer tbCrmCustomer = tbCrmCustomerService.selectCustomerById(id);
        TbCrmContacts tbCrmContacts = tbCrmContactsService.selectConById(id);
        modelMap.addAttribute("customerVO", tbCrmCustomer);
        modelMap.addAttribute("contactsVO", tbCrmContacts);
        session.setAttribute("customerVO", tbCrmCustomer);
        return "/views/manage/customer/show";
    }

    //去编辑客户的页面
    @RequestMapping("/toCustomerEdit")
    public String customerEdit(Integer id, ModelMap modelMap) {
        TbCrmCustomer tbCrmCustomer = tbCrmCustomerService.selectCustomerById(id);
        modelMap.addAttribute("customerVO", tbCrmCustomer);
        List<TbSystemUser> search = userMapper.search();
        modelMap.addAttribute("userList", search);
        return "/views/manage/customer/edit";
    }

    //提交编辑客户的信息
    @RequestMapping("/customerEdit")
    @ResponseBody
    public Map customerEdit(TbCrmCustomer tbCrmCustomer) {
        System.out.println(tbCrmCustomer);
        Map map = new HashMap();
        tbCrmCustomerService.updateById(tbCrmCustomer);//mybatisPlus方法
        map.put("msg", "修改成功");
        return map;
    }

    //去添加页面
    @RequestMapping("/toAddUser")
    public String toAdd(ModelMap map) {
        List<TbSystemUser> search = userMapper.search();
        map.addAttribute("userList", search);
        return "/views/manage/customer/add";
    }

    //执行添加操作
    @RequestMapping("/insert")
    @ResponseBody
    public ModelMap save(TbCrmCustomer crmCustomer, TbCrmContacts crmContacts) {
        ModelMap map = new ModelMap();
        //插入客户表信息
        boolean insert_Customer = tbCrmCustomerService.save(crmCustomer);
        //插入联系人表信息
        boolean insert_Contacts = tbCrmContactsService.save(crmContacts);

        if (insert_Customer && insert_Contacts) {
            map.addAttribute("msg", "添加成功！");
        } else {
            map.addAttribute("msg", "添加失败，注意细节~");
        }
        return map;
    }

    //去写邮箱页面
    @RequestMapping("/toEmail")
    public String toMessageAction_write(String rows, HttpSession session) {
        session.setAttribute("zipCode", rows);
        return "/views/manage/customer/sendemail";
    }

    //接收\发送邮箱内容
    @RequestMapping("/sendEmail")
    @ResponseBody
    public Map MarketingActionSendEmail(HttpSession session, String subject, String content) {
        String zipCode = (String) session.getAttribute("zipCode");
        Map htmlMail = emailService.sendHtmlMail(zipCode, subject, content);
        return htmlMail;
    }

    //去发短信界面
    @RequestMapping("/toChat")
    public String toChat(Integer rows, HttpSession session) {
        ModelMap modelMap = new ModelMap();
        System.out.println(rows);
        TbCrmCustomer tbCrmCustomer = crmCustomerMapper.selectById(rows);
        Integer ownerUserId = tbCrmCustomer.getOwnerUserId();
        TbCrmContacts tbCrmContacts = tbCrmContactsService.selectConById(rows);
        System.out.println(tbCrmContacts.getTelephone());
        modelMap.addAttribute("tbCrmContacts", tbCrmContacts);
        session.setAttribute("tbCrmContacts", tbCrmContacts.getTelephone());
        return "views/manage/customer/sendechat";
    }

    //接收\发送短信内容
    @RequestMapping("/sendChat")
    @ResponseBody
    public Map sendChat(String toAddr, String content) {
        Map map = new HashMap();
        System.out.println(toAddr);
        System.out.println(content);
        Integer send = SendMessageUtil.send("水水水水", "d41d8cd98f00b204e980", toAddr, content);
        if (send > 0) {
            map.put("success", "发送成功");
        }
        return map;
    }

    /*//今日新建
    @RequestMapping("/todayCreate")
    public String todayCreate(@RequestParam("type") String type, HttpSession session) {
        session.setAttribute("type1", type);
        return "forward:/toList";
    }

    //本周新建
    @RequestMapping("/weekCreate")
    public String weekCreate(@RequestParam("type") String type, HttpSession session) {
        session.setAttribute("type2", type);
        return "forward:/toList";
    }

    //本月新建
    @RequestMapping("/monthCreate")
    public String monthCreate(@RequestParam("type") String type, HttpSession session) {
        session.setAttribute("type3", type);
        return "forward:/toList";
    }*/

    //本日、周、月新建
    @RequestMapping("/createTime")
    public String createTime(@RequestParam("type") String type, HttpSession session) {
        //从前端过来的type类型存入session  （day、week、month）
        session.setAttribute("type", type);
        return "forward:/toList"; //转发到去展示页面的控制器
    }

    //删除客户
    @RequestMapping("/toCustomerDelete")
    public String toCustomerDelete(Integer id) {
        int i = crmCustomerMapper.deleteById(id);//逻辑删除
        return "forward:/toList";//删除完毕转发到去展示页面的控制器
    }

    //删除客户池
    @RequestMapping("/toCustomerPoolDelete")
    public String toCustomerPoolDelete(Integer id) {
        int i = crmCustomerMapper.deleteById(id);//逻辑删除
        return "forward:/toPool";//删除完毕转发到去展示客户池的控制器
    }
}
